<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Python," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="Python 元组&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;元组是序列的一种。序列包括字符串、列表和元组。 序列说明&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;序列的两个主要特点是索引操作符和切片操作符  索引操作符让我们可以从序列中抓取一个特定项目 切片操作符让我们能够获取序列的一个切片，即">
<meta name="keywords" content="Python">
<meta property="og:type" content="article">
<meta property="og:title" content="Python 元组">
<meta property="og:url" content="https://hcldirgit.github.io/2017/08/10/Python/1. Python 基础篇/1. Python 基础语法/1.6 元组/8. Python 元组/index.html">
<meta property="og:site_name" content="失落的乐章">
<meta property="og:description" content="Python 元组&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;元组是序列的一种。序列包括字符串、列表和元组。 序列说明&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;序列的两个主要特点是索引操作符和切片操作符  索引操作符让我们可以从序列中抓取一个特定项目 切片操作符让我们能够获取序列的一个切片，即">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-06-19T13:38:04.857Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Python 元组">
<meta name="twitter:description" content="Python 元组&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;元组是序列的一种。序列包括字符串、列表和元组。 序列说明&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;序列的两个主要特点是索引操作符和切片操作符  索引操作符让我们可以从序列中抓取一个特定项目 切片操作符让我们能够获取序列的一个切片，即">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://hcldirgit.github.io/2017/08/10/Python/1. Python 基础篇/1. Python 基础语法/1.6 元组/8. Python 元组/"/>





  <title>Python 元组 | 失落的乐章</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', '85*****1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?87980c**************99ec5e26fb5";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>











  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">失落的乐章</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">技术面前，永远都是学生。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-external-link"></i> <br />
            
            留言
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://hcldirgit.github.io/2017/08/10/Python/1. Python 基础篇/1. Python 基础语法/1.6 元组/8. Python 元组/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="失落的乐章">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/0.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="失落的乐章">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Python 元组</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-08-10T14:00:36+08:00">
                2017-08-10
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="Python-元组"><a href="#Python-元组" class="headerlink" title="Python 元组"></a>Python 元组</h1><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;元组是序列的一种。序列包括字符串、列表和元组。</p>
<h2 id="序列说明"><a href="#序列说明" class="headerlink" title="序列说明"></a>序列说明</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;序列的两个主要特点是索引操作符和切片操作符</p>
<ul>
<li>索引操作符让我们可以从序列中抓取一个特定项目</li>
<li>切片操作符让我们能够获取序列的一个切片，即一部分序列。</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;序列的基本操作</p>
<ul>
<li>len()：求序列的长度</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">1</span>]: a = <span class="string">'abc'</span></div><div class="line"></div><div class="line">In [<span class="number">2</span>]: len(a)</div><div class="line">Out[<span class="number">2</span>]: <span class="number">3</span></div></pre></td></tr></table></figure>
<ul>
<li>+：连接2个序列</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">3</span>]: a + <span class="string">'d'</span></div><div class="line">Out[<span class="number">3</span>]: <span class="string">'abcd'</span></div></pre></td></tr></table></figure>
<ul>
<li>*：重复序列元素</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">4</span>]: a</div><div class="line">Out[<span class="number">4</span>]: <span class="string">'abc'</span></div><div class="line"></div><div class="line">In [<span class="number">5</span>]: a * <span class="number">4</span></div><div class="line">Out[<span class="number">5</span>]: <span class="string">'abcabcabcabc'</span></div><div class="line"></div><div class="line">In [<span class="number">8</span>]: <span class="string">'@'</span> * <span class="number">10</span></div><div class="line">Out[<span class="number">8</span>]: <span class="string">'@@@@@@@@@@'</span></div></pre></td></tr></table></figure>
<ul>
<li>in 或 not in：判断元素是否存在序列中</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">9</span>]: <span class="string">'a'</span> <span class="keyword">in</span> a</div><div class="line">Out[<span class="number">9</span>]: <span class="keyword">True</span></div><div class="line"></div><div class="line">In [<span class="number">10</span>]: a</div><div class="line">Out[<span class="number">10</span>]: <span class="string">'abc'</span></div><div class="line"></div><div class="line">In [<span class="number">11</span>]: <span class="string">'d'</span> <span class="keyword">in</span> a</div><div class="line">Out[<span class="number">11</span>]: <span class="keyword">False</span></div><div class="line"></div><div class="line">In [<span class="number">12</span>]: <span class="string">'d'</span> <span class="keyword">not</span> <span class="keyword">in</span> a</div><div class="line">Out[<span class="number">12</span>]: <span class="keyword">True</span></div></pre></td></tr></table></figure>
<ul>
<li>max()：返回最大值</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">13</span>]: max(a)</div><div class="line">Out[<span class="number">13</span>]: <span class="string">'c'</span></div></pre></td></tr></table></figure>
<ul>
<li>min()：返回最小值</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">14</span>]: min(a)</div><div class="line">Out[<span class="number">14</span>]: <span class="string">'a'</span></div></pre></td></tr></table></figure>
<ul>
<li>cmp(x,y)：比较两个个序列是否相等</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">15</span>]: a</div><div class="line">Out[<span class="number">15</span>]: <span class="string">'abc'</span></div><div class="line"></div><div class="line">In [<span class="number">16</span>]: cmp(a,<span class="string">'abc'</span>)</div><div class="line">Out[<span class="number">16</span>]: <span class="number">0</span></div><div class="line"></div><div class="line">In [<span class="number">17</span>]: cmp(a,<span class="string">'abcd'</span>)</div><div class="line">Out[<span class="number">17</span>]: <span class="number">-1</span></div><div class="line"></div><div class="line">In [<span class="number">18</span>]: cmp(a,<span class="string">'ab'</span>)</div><div class="line">Out[<span class="number">18</span>]: <span class="number">1</span></div><div class="line"></div><div class="line">In [<span class="number">19</span>]: cmp(a+<span class="string">'g'</span>,<span class="string">'abcdef'</span>)</div><div class="line">Out[<span class="number">19</span>]: <span class="number">1</span></div></pre></td></tr></table></figure>
<h2 id="元组"><a href="#元组" class="headerlink" title="元组"></a>元组</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 的元组与列表十分相似，不同之处在于元组的元素不能修改。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;元组和字符串一样是不可变的</p>
<ul>
<li>元组可以存储一系列的值</li>
<li>元组通常用在用户定义的函数能够安全地采用一组值的时候，即被使用的元组的值不回改变</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;元组使用小括号，列表使用方括号。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;元组创建很简单，只需要在括号中添加元素，并使用逗号隔开即可。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">21</span>]: tup1 = (<span class="string">'python'</span>,<span class="string">'a'</span>,<span class="number">1997</span>,<span class="number">2017</span>);</div><div class="line"></div><div class="line">In [<span class="number">22</span>]: type(tup1)</div><div class="line">Out[<span class="number">22</span>]: tuple</div><div class="line"></div><div class="line">In [<span class="number">23</span>]: tup2 = (<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,(<span class="number">5</span>,))</div><div class="line"></div><div class="line">In [<span class="number">24</span>]: type(tup2)</div><div class="line">Out[<span class="number">24</span>]: tuple</div><div class="line"></div><div class="line">In [<span class="number">25</span>]: tup3 = <span class="string">"a"</span>,<span class="string">"b"</span>,<span class="string">"c"</span>,<span class="string">"d"</span>;</div><div class="line"></div><div class="line">In [<span class="number">26</span>]: type(tup3)</div><div class="line">Out[<span class="number">26</span>]: tuple</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建空元组</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">27</span>]: tup1 = ();</div><div class="line"></div><div class="line">In [<span class="number">28</span>]: type(tup1)</div><div class="line">Out[<span class="number">28</span>]: tuple</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;元组中只包含一个元素时，需要在元素后面添加逗号</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">29</span>]: tup1 = (<span class="number">50</span>,);</div><div class="line"></div><div class="line">In [<span class="number">30</span>]: type(tup1)</div><div class="line">Out[<span class="number">30</span>]: tuple</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;元组与字符串类似，下标索引从0开始，可以进行截取，组合等。</p>
<h2 id="访问元组"><a href="#访问元组" class="headerlink" title="访问元组"></a>访问元组</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;元组可以使用下标索引来访问元组中的值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"></div><div class="line">tup1 = (<span class="string">'physics'</span>, <span class="string">'chemistry'</span>, <span class="number">1997</span>, <span class="number">2000</span>);</div><div class="line">tup2 = (<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span> );</div><div class="line"></div><div class="line"><span class="keyword">print</span> <span class="string">"tup1[0]: "</span>, tup1[<span class="number">0</span>]</div><div class="line"><span class="keyword">print</span> <span class="string">"tup2[1:5]: "</span>, tup2[<span class="number">1</span>:<span class="number">5</span>]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输出结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">tup1[<span class="number">0</span>]:  physics</div><div class="line">tup2[<span class="number">1</span>:<span class="number">5</span>]:  (<span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>)</div></pre></td></tr></table></figure>
<h2 id="修改元组"><a href="#修改元组" class="headerlink" title="修改元组"></a>修改元组</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;元组的元素值是不允许修改的，但可以对元组进行连接组合</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></div><div class="line"></div><div class="line">tup1 = (<span class="number">12</span>, <span class="number">34.56</span>);</div><div class="line">tup2 = (<span class="string">'abc'</span>, <span class="string">'xyz'</span>);</div><div class="line"></div><div class="line"><span class="comment"># 以下修改元组元素操作是非法的。</span></div><div class="line"><span class="comment"># tup1[0] = 100;</span></div><div class="line"></div><div class="line"><span class="comment"># 创建一个新的元组</span></div><div class="line">tup3 = tup1 + tup2;</div><div class="line"><span class="keyword">print</span> tup3;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输出结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">(<span class="number">12</span>,<span class="number">34</span>,<span class="number">56</span>,<span class="string">'abc'</span>,<span class="string">'xyz'</span>)</div></pre></td></tr></table></figure>
<h2 id="删除元组"><a href="#删除元组" class="headerlink" title="删除元组"></a>删除元组</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;元组中的元素值是不允许删除的，但可以使用 del 语句来删除整个元组</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"></div><div class="line">tup = (<span class="string">'physics'</span>, <span class="string">'chemistry'</span>, <span class="number">1997</span>, <span class="number">2000</span>);</div><div class="line"></div><div class="line"><span class="keyword">print</span> tup;</div><div class="line"><span class="keyword">del</span> tup;</div><div class="line"><span class="keyword">print</span> <span class="string">"After deleting tup : "</span></div><div class="line"><span class="keyword">print</span> tup;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上实例元组被删除后，输出变量会有异常信息，输出如下所示：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">(<span class="string">'physics'</span>, <span class="string">'chemistry'</span>, <span class="number">1997</span>, <span class="number">2000</span>)</div><div class="line">After deleting tup :</div><div class="line">Traceback (most recent call last):</div><div class="line">  File <span class="string">"test.py"</span>, line <span class="number">9</span>, <span class="keyword">in</span> &lt;module&gt;</div><div class="line">    <span class="keyword">print</span> tup;</div><div class="line">NameError: name <span class="string">'tup'</span> <span class="keyword">is</span> <span class="keyword">not</span> defined</div></pre></td></tr></table></figure>
<h2 id="元组运算符"><a href="#元组运算符" class="headerlink" title="元组运算符"></a>元组运算符</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;与字符串一样，元组之间可以使用 + 号和 <em> 号进行元算。这就意味着它们可以组合和复制，运算后会生成一个新的元组。<br>python表达式|结果|描述<br>—|—|—<br>len((1,2,3))|3|计算元素个数<br>（1，2，3） + （4，5，6）|（1，2，3，4，5，6）|连接<br>(‘Hi!’) </em> 4|(‘Hi!’,’Hi!’,’Hi!’,’Hi!’)|复制<br>3 in (1,2,3)|true|元素是否存在<br>for x in (1,2,3):print x,|1 2 3 |迭代</p>
<h2 id="元组索引、截取"><a href="#元组索引、截取" class="headerlink" title="元组索引、截取"></a>元组索引、截取</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;因为元组也是一个序列，所以可以访问元组中的指定位置的元素，也可以截取索引中的一段元素</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">L = (<span class="string">'spam'</span>,<span class="string">'Spam'</span>,<span class="string">'SPAM!'</span>)</div></pre></td></tr></table></figure>
<table>
<thead>
<tr>
<th>python表达式</th>
<th>结果</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>L[2]</td>
<td>‘SPANM!’</td>
<td>读取第三个元素</td>
</tr>
<tr>
<td>L[-2]</td>
<td>‘Spam’</td>
<td>反向读取；读取倒数第二个元素</td>
</tr>
<tr>
<td>L[1:]</td>
<td>(‘Spam’,’SPAM!’)</td>
<td>截取元素</td>
</tr>
</tbody>
</table>
<h2 id="无关闭分隔符"><a href="#无关闭分隔符" class="headerlink" title="无关闭分隔符"></a>无关闭分隔符</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;任意无符号的对象，以逗号隔开，默认为元组</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"></div><div class="line"><span class="keyword">print</span> <span class="string">'abc'</span>, <span class="number">-4.24e93</span>, <span class="number">18</span>+<span class="number">6.6j</span>, <span class="string">'xyz'</span>;</div><div class="line">x, y = <span class="number">1</span>, <span class="number">2</span>;</div><div class="line"><span class="keyword">print</span> <span class="string">"Value of x , y : "</span>, x,y;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上实例运行结果：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">abc <span class="number">-4.24e+93</span> (<span class="number">18</span>+<span class="number">6.6j</span>) xyz</div><div class="line">Value of x , y : <span class="number">1</span> <span class="number">2</span></div></pre></td></tr></table></figure>
<h2 id="元组内置函数"><a href="#元组内置函数" class="headerlink" title="元组内置函数"></a>元组内置函数</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;python元组包含了以下内置函数</p>
<table>
<thead>
<tr>
<th>序号</th>
<th>方法及描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/%E5%85%83%E7%BB%84%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0/1.%20Python%20Tuple(%E5%85%83%E7%BB%84" target="_blank" rel="external">cmp(tuple1, tuple2)</a>%20cmp()%E6%96%B9%E6%B3%95.md)，比较两个元组元素。</td>
</tr>
<tr>
<td>2</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/%E5%85%83%E7%BB%84%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0/2.%20Python%20Tuple(%E5%85%83%E7%BB%84" target="_blank" rel="external">len(tuple)</a>%20len()%E6%96%B9%E6%B3%95.md)，计算元组元素个数。</td>
</tr>
<tr>
<td>3</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/%E5%85%83%E7%BB%84%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0/3.%20Python%20Tuple(%E5%85%83%E7%BB%84" target="_blank" rel="external">max(tuple)</a>%20max()%E6%96%B9%E6%B3%95.md)，返回元组中元素最大值。</td>
</tr>
<tr>
<td>4</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/%E5%85%83%E7%BB%84%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0/4.%20Python%20Tuple(%E5%85%83%E7%BB%84" target="_blank" rel="external">min(tuple)</a>%20min()%E6%96%B9%E6%B3%95.md)，返回元组中元素最小值。</td>
</tr>
<tr>
<td>5</td>
<td><a href="https://github.com/hcldirgit/studygit/blob/master/%E5%85%83%E7%BB%84%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0/5.%20Python%20Tuple(%E5%85%83%E7%BB%84" target="_blank" rel="external">tuple(seq)</a>%20tuple()%E6%96%B9%E6%B3%95.md)，将列表转换为元组。</td>
</tr>
</tbody>
</table>
<h2 id="元组的拆分"><a href="#元组的拆分" class="headerlink" title="元组的拆分"></a>元组的拆分</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以通过变量去接收元组的值,通常叫做元组的拆分</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">In [<span class="number">31</span>]: tup1 = (a,<span class="string">'b'</span>,<span class="string">'c'</span>)</div><div class="line"></div><div class="line">In [<span class="number">32</span>]: tup1</div><div class="line">Out[<span class="number">32</span>]: (<span class="string">'abc'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>)</div><div class="line"></div><div class="line">In [<span class="number">33</span>]: first,second,third = tup1</div><div class="line"></div><div class="line">In [<span class="number">34</span>]: first</div><div class="line">Out[<span class="number">34</span>]: <span class="string">'abc'</span></div><div class="line"></div><div class="line">In [<span class="number">35</span>]: second</div><div class="line">Out[<span class="number">35</span>]: <span class="string">'b'</span></div><div class="line"></div><div class="line">In [<span class="number">36</span>]: third</div><div class="line">Out[<span class="number">36</span>]: <span class="string">'c'</span></div></pre></td></tr></table></figure>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Python/" rel="tag"># Python</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/08/10/Python/1. Python 基础篇/1. Python 基础语法/1.9 字典练习/11. Python 字典练习/" rel="next" title="Python 字典练习">
                <i class="fa fa-chevron-left"></i> Python 字典练习
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/08/10/Python/1. Python 基础篇/1. Python 基础语法/2.0 2.1 流程控制-if条件/12. Python 流程控制-if 条件语句/" rel="prev" title="Python 流程控制- if 条件语句">
                Python 流程控制- if 条件语句 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/0.png"
               alt="失落的乐章" />
          <p class="site-author-name" itemprop="name">失落的乐章</p>
           
              <p class="site-description motion-element" itemprop="description">失落的乐章的Blog</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/">
                <span class="site-state-item-count">627</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">38</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/hcldirgit" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Python-元组"><span class="nav-number">1.</span> <span class="nav-text">Python 元组</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#序列说明"><span class="nav-number">1.1.</span> <span class="nav-text">序列说明</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#元组"><span class="nav-number">1.2.</span> <span class="nav-text">元组</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#访问元组"><span class="nav-number">1.3.</span> <span class="nav-text">访问元组</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#修改元组"><span class="nav-number">1.4.</span> <span class="nav-text">修改元组</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#删除元组"><span class="nav-number">1.5.</span> <span class="nav-text">删除元组</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#元组运算符"><span class="nav-number">1.6.</span> <span class="nav-text">元组运算符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#元组索引、截取"><span class="nav-number">1.7.</span> <span class="nav-text">元组索引、截取</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#无关闭分隔符"><span class="nav-number">1.8.</span> <span class="nav-text">无关闭分隔符</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#元组内置函数"><span class="nav-number">1.9.</span> <span class="nav-text">元组内置函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#元组的拆分"><span class="nav-number">1.10.</span> <span class="nav-text">元组的拆分</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">失落的乐章</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
